package com.moana.buyTicket.servlet;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moana.buyTicket.cache.redis.TicketMapper;
import com.moana.buyTicket.cache.redis.TranFlowMapper;
import com.moana.buyTicket.entity.Person;
import com.moana.buyTicket.entity.Ticket;
import com.moana.buyTicket.entity.TranFlow;
import com.moana.buyTicket.util.HttpAnno;
import com.moana.buyTicket.util.ServletUtil;
import com.moana.buyTicket.util.email.MailSenderInfo;
import com.moana.buyTicket.util.email.SimpleMailSender;

@Controller
public class TicketAction {
	
	private static Integer count = 0; 
	
	@RequestMapping(value="/showTicket.htm")
	@HttpAnno(value="AJAX")
	@ResponseBody
	public static String showTicket(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		TicketMapper mapper = new TicketMapper();
		List<Ticket> tickets =  mapper.getTickets(0, 20);
		Map<String, Object> map = new HashMap<String, Object>();
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			
			String ticketsJson = objectMapper.writeValueAsString(tickets);
			map.put("errorCode", "0");
			map.put("data", ticketsJson);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			map.put("errorCode", "1");
			map.put("errorMessage", "解析json数据错误" );
		}
		String jsonString = null;
		try {
			jsonString = objectMapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonString;
	}
	
	@RequestMapping(value="/getTicket.htm",produces = "text/html;charset=UTF-8")
	@HttpAnno(value="AJAX")
	@ResponseBody
	public static String getTicket(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		synchronized (request) {
			TicketMapper mapper = new TicketMapper();
			HttpSession session = request.getSession(); 
			Person person = (Person)session.getAttribute("user");
			Integer personID = person.getId();
			ObjectMapper objectMapper = new ObjectMapper();
			Map<String, Object> map = new HashMap<String, Object>();
			List<Ticket> tickets = mapper.getTickets(0, 1);
			if(tickets.size()==0){
				map.put("errorCode", "1" );
				map.put("errorMessage", "电影票已抢购空！！！");
				String jsonString = null;
				try {
					jsonString = objectMapper.writeValueAsString(map);
				} catch (JsonProcessingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return jsonString;
			}
			else{
				Ticket ticket = tickets.get(0);
				mapper.delete(ticket);
				ticket.setStatus(1);
				mapper.addSus(ticket, count);
				TranFlowMapper tranMapper = new TranFlowMapper();
				TranFlow tranFlow = new TranFlow();
				tranFlow.setId(count);
				tranFlow.setPersonID(personID);
				Integer ticketId = ticket.getId();
				tranFlow.setTicketID(ticketId);
				Date date = new Date(System.currentTimeMillis());
			    tranFlow.setSendTime(date);
				tranFlow.setStatus(1);
				tranMapper.add(tranFlow, count);
				ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
				MailSenderInfo sendInfo = (MailSenderInfo)ctx.getBean("sendInfo");
				sendInfo.setSubject("抢票系统提醒");
				String mailContent = "您购买的电影票:"+ticket.getId()+"座位："+ticket.getLine()+"行"+ticket.getRow()+"排";
				sendInfo.setContent(mailContent);
				SimpleMailSender mailSender = new SimpleMailSender();
				try {
					mailSender.sendTextMailSSL(sendInfo);
				} catch (GeneralSecurityException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				count++;
				map.put("errorCode", "0");
				map.put("errorMessage", "抢购成功");
				String jsonString = null;
				try {
					jsonString = objectMapper.writeValueAsString(map);
				} catch (JsonProcessingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return jsonString;
			}
		}
		
	}

	
}
